package admission.ecnu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import admission.ecnu.dao.classInfoDao;
import admission.ecnu.dao.teacherInfoDao;
import admission.ecnu.interfaces.IRnsService;
import admission.ecnu.mainservlet.RnsContext;
import admission.ecnu.object.DataManager;
import admission.ecnu.object.OClassInfo;
import admission.ecnu.object.OTeacherInfo;
import antlr.collections.List;
public class inputClassInfo implements IRnsService {

	//private OClassInfo newClass = null;
	@Override
	public void executeService(RnsContext context) throws Exception {
		/*
		 * 验证用户key值
		 */
		
		System.out.print("class info:"+context.getJsonRequestObject());
		
		
		JSONObject obj = context.getJsonRequestObject();
		String classInfo = obj.getString("classInfo");
		classInfo = new String(classInfo.getBytes("iso-8859-1"),"utf-8"); 
		
		
		if(!DataManager.USER_ID.equals(obj.get("key"))){
			JSONObject jsonReturn = new JSONObject();
			jsonReturn.put("returnCode", DataManager.RETURN_WRONGKEY);
			context.setJsonResponseObject(jsonReturn);
			return;
		}
		
		new classInfoDao().classDelete();
		
		String[] classStrArray = classInfo.split("[$]");
		ArrayList<OClassInfo> classList= new ArrayList<OClassInfo>();
		
		for(int i = 0; i< classStrArray.length; i++){
			OClassInfo newClass = new OClassInfo();
			System.out.print("class:"+i+classStrArray[i]+"\n");
			String[] item = classStrArray[i].split(",");
			for(int j = 0; j<item.length; j++){
				String[] keyValue = item[j].split("=");
				if(keyValue[0].equals("classId"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setClassId("");
					else {
						newClass.setClassId(keyValue[1]);
					}
				if(keyValue[0].equals("grade"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setGrade("");
					else {
						newClass.setGrade(keyValue[1]);
					}
				if(keyValue[0].equals("curriculum"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setCurriculum("");
					else {
						newClass.setCurriculum(keyValue[1]);
					}
				if(keyValue[0].equals("studentNum"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setStudentNum("50");
					else {
						newClass.setStudentNum(keyValue[1]);
					}
				if(keyValue[0].equals("type"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setType("");
					else {
						newClass.setType(keyValue[1]);
					}
				if(keyValue[0].equals("teacher"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setTeacher("");
					else {
						newClass.setTeacher(keyValue[1].trim());
					}
				if(keyValue[0].equals("classTime"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setClassTime("");
					else {
						newClass.setClassTime(keyValue[1]);
					}
				if(keyValue[0].equals("classroom"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setClassroom("");
					else {
						newClass.setClassroom(keyValue[1]);
					}
				if(keyValue[0].equals("isExam"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setIsExam("");
					else {
						newClass.setIsExam(keyValue[1]);
					}
				if(keyValue[0].equals("classDay"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setClassDay("周一");
					else {
						newClass.setClassDay(keyValue[1]);
					}
				if(keyValue[0].equals("classCapacity"))
					if(keyValue.length<2||keyValue[1].length()==0||keyValue[1] == null)
						newClass.setClassroomCapacity("");
					else {
						newClass.setClassroomCapacity(keyValue[1]);
					}
			}
			classList.add(newClass);
			System.out.print("new class"+i+": "+classList.toString()+"\n");
			
			newClass = null;
		}
		
		java.util.List<OTeacherInfo> teacherList = new teacherInfoDao().teacherSelect();
		
		Map<String, String>teacherMap = new HashMap<String, String>();
		for(OTeacherInfo teacher:teacherList){
			System.out.print("teacher:"+teacher);
			teacherMap.put(teacher.getName().trim(), teacher.getTeacherId());
		}
		
		new classInfoDao().classInsert(classList,teacherMap);
		
		JSONObject jsonReturn = new JSONObject();
		jsonReturn.put("returnCode", DataManager.RETURN_SUCCESSFUL);
		context.setJsonResponseObject(jsonReturn);
	}
}